﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace YmatouSeller.Notice.API.Models
{
    public class MyHelp
    {
        private static string Get8CharKey(string oKey)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(oKey);

            MD5CryptoServiceProvider mp = new MD5CryptoServiceProvider();
            byte[] hashKey = mp.ComputeHash(bytes);

            string result = Encoding.ASCII.GetString(hashKey);

            return result.Substring(0, 8);
        }
        private static string EncryptKey
        {
            get { return "ymatou.com"; }
        }

        /// <summary>
        /// 进行DES加密。密钥是当前会状态
        /// </summary>
        /// <param name="pToEncrypt">要加密的字符串。</param>
        /// <param name="dykey">密钥。</param>
        /// <returns>以Base64格式返回的加密字符串。</returns>
        public static string Encrypt(string pToEncrypt, string dykey)
        {
            if (string.IsNullOrEmpty(pToEncrypt))
            {
                return pToEncrypt;
            }
            //int dykey = SellerID;
            string encryptKey = string.Format("{0}{1}", EncryptKey, dykey);
            string sKey = Get8CharKey(encryptKey);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }

                //Convert.ToBase64String(ms.ToArray())

                string str = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return str;
            }
        }
        /// <summary>
        /// 进行DES解密。密钥是当前会状态。
        /// </summary>
        /// <param name="pToDecrypt">要解密的以Base64</param>
        /// <param name="dykey">密钥。</param>
        /// <returns>已解密的字符串。</returns>
        public static string Decrypt(string pToDecrypt, string dykey)
        {
            if (string.IsNullOrEmpty(pToDecrypt))
            {
                return pToDecrypt;
            }
            string encryptKey = string.Format("{0}{1}", EncryptKey, dykey);
            string sKey = Get8CharKey(encryptKey);
            byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
        }

    }
}